import java.security.spec.RSAOtherPrimeInfo;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [][] array ={{1,2,3},{4,5,6}};

        /*
        System.out.println(array[1][1]);//5
        System.out.println(array[1][0]);//4*/

        for (int i = 0; i< 2; i++ ){
            for (int j = 0; j< 3; j++){
                System.out.println(array[i][j] + " ");
            }
            System.out.println();
        }
        int [][] array2 =new int[][] {{1,2,3},{4,5,6}};
        //Java 当中的二维数组是可以省略的
        int [][] array3 =new int[2][3];
    }
    public static void transform1(int[] array) {
        for (int i =0; i < array.length; i++) {
            array[i] =array[1] * 2;
        }
    }

    public static int[] transform2(int[] array) {
        int[] tmp = new int [array.length];
        for (int i =0; i < array.length; i++) {
            tmp[i] = array[i] * 2;
        }
       return tmp;
    }

    public static void func(int[] array) {
        int i = 0;
        int j = array.length-1;
        while (i < j) {
            while (i < j && array[i] % 2 !=0) {
                i++;
            }
            while (i < j && array[j] % 2 ==0) {
                j--;
            }
        }
        swap(array,i,j);
    }
    public static int[] twoIndex(int[] array,int target) {

        int[] tmp = {-1,-1};
        for (int i =0; i < array.length; i++){
            for (int j =0; j < array.length; j++){
                if (array[i] + array[j] == target) {
                   tmp[0] = i;
                   tmp[1] = j;
                }
            }
        }
        return tmp;
    }


    public static int singleNem(int[] array) {
        int ret =0;
        for (int i =0; i < array.length; i++){
            ret ^=array[i];
        }
        return ret;
    }

    public static int moreNum(int[] array) {
        Arrays.sort(array);
        return array[array.length/2];
    }
    public static boolean function(int[] array) {
        int count = 0;
        for (int i =0;i < array.length; i++) {
           if (array[i] % 2 !=0){
               //遇到奇数了
            count++;
            if(count == 3)  {
                return true;
            }
           } else{
               //遇到偶数了
               count= 0;
           }
        }
       return false;
    }
    public static void main12(String[] args) {
        int[] array = {1, 3};
        boolean flg = function(array);
        System.out.println(flg);

        /*int ret = singleNum(array);
        System.out.println(ret);*/
        /*int [] ret = twoIndex(array,18);
        System.out.println(Arrays.toString(ret));*/
    }

    public static void main11(String[] args) {
        int[] array = {1, 3, 5};

        func(array);

        System.out.println(Arrays.toString(array));
    }


    public static void main10(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        //transform1(array);
        int [] ret = transform2(array);
        System.out.println(Arrays.toString(array));
        System.out.println("ret: "+Arrays.toString(ret));
    }


    public static void main9(String[] args) {
        int[] array1 = {1, 21, 31, 3, 5};
        int[] array2 = {1, 21, 31, 3, 5};
        boolean flg =Arrays.equals(array1,array2);
        System.out.println(flg);

        System.out.println("======fill=======");
        int[] array3 = new int [10];
        //Arrays.fill(3,9);
        Arrays.fill(array3,2,5,8);
        System.out.println(Arrays.toString(array3));

    }
    public static int find(int[] array,int key) {
      for (int i =0;i < array.length; i++){
           if (array [i] == key) {
               return i;
           }
      }
      return -1;
    }
    public static int binarySearch(int[] array,int key) {

        int left = 0;
        int right = array.length-1;

        while (left<= right){
            int mid = (left+right)/2;
            if(array[mid]< key) {
                left = mid+1;

            }else if (array[mid]== key){
                return mid;
            }else{
                right = mid-1;
            }
        }
        return -1;
    }
    private static void swap(int [] array,int i,int j){
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }

    public static void bubbleSort(int[] array) {
        for (int i =0; i < array.length-1; i++) {
            boolean flg = false;
            //- i 就是一种优化，每一趟比上一趟上比较1次
            for (int j =0;j < array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if(flg == false){
                break;
            }
        }
    }

    public static void main8(String[] args) {
        int[] array = {1, 21, 31, 3, 5};
        //bubbleSort(array);
        //Arrays.sort(array,2,5);
        Arrays.sort(array);

        System.out.println("排序之后的数组："+Arrays.toString(array));

        int index= Arrays.binarySearch(array,13);
        //int index=find(array,3);
        //int index=binarySearch(array,3);
        if(index == -1){
            System.out.println("没有找到你想要的数字！");
        }else{
            System.out.println("找到了你想要的数字！"+index);
        }
    }
    public static void main7(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        //Java当中，一般下标给定范围的时候[1，3)
        int [] copy = Arrays.copyOfRange(array,1,3);
        System.out.println(Arrays.toString(array));
    }

    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int [] copy = Arrays.copyOf(array,array.length*2);
        array = copy;
        for (int x:copy) {
            System.out.println(x+" ");
        }
        System.out.println();
        System.out.println(Arrays.toString(array));
    }
    public static void main5(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int [] copy = new int[array.length];

        //帮我们完了拷贝
        System.arraycopy(array,0,copy,0,array.length);
        for (int x:copy) {
            System.out.println(x+" ");
        }
        System.out.println();
    }


    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int [] copy = Arrays.copyOf(array,array.length);
        for (int x:copy) {
            System.out.println(x+" ");
        }
        System.out.println();
    }


    public static void main3(String[] args) {
        //这个代码不是拷贝
        int[] array = {1, 2, 3, 4, 5};
        int[] array2 =array;
        for (int x:array2) {
            System.out.println(x + " ");
        }

        System.out.println();
    }
    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4, 5};//我们要拷贝一个数组
        int [] copy = new int[array.length];
        for (int i =0;i < array.length; i++) {
            copy[i]=array[i];
        }
        for (int x:copy) {
            System.out.println(x+" ");
        }
        System.out.println();
    }
    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4, 5};//用下标的形式访问他，从零开始，范围是[0，5)不能取到5
        //第一种方式，开始遍历，用for循环
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }//在遍历过程中，我们使用数组名 .length来获取当前数组的长度。
        System.out.println();

        //第二种方式
        System.out.println("==============");
        for (int x : array) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("================");

        //第三种方式
        String ret = Arrays.toString(array);
        System.out.println(ret);
        //这种方式相当于将字符串以数组的方式输出
    }
}
